package com.rdzn.mall.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rdzn.mall.center.entity.PmsProductHot;
import com.rdzn.mall.center.mapper.PmsProductHotMapper;
import com.rdzn.mall.center.service.IPmsProductHotService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: pms_product_hot
 * @Author: jeecg-boot
 * @Date:   2020-10-13
 * @Version: V1.0
 */
@Service
public class PmsProductHotServiceImpl extends ServiceImpl<PmsProductHotMapper, PmsProductHot> implements IPmsProductHotService {

    @Override
    public PmsProductHot refresh(Integer productId) {
        PmsProductHot hot = getOne(new LambdaQueryWrapper<PmsProductHot>().eq(PmsProductHot::getProductId, productId));
        if (hot == null){
            hot = new PmsProductHot();
            hot.setProductId(productId);
            hot.setHot(1);
            hot.setCreateTime(new Date());
            super.save(hot);
        }else {
            hot.setHot(hot.getHot()+1);
            super.updateById(hot);
        }
        return hot;
    }

    @Override
    public PmsProductHot getByProductId(Integer productId) {
        return getOne(new LambdaQueryWrapper<PmsProductHot>().eq(PmsProductHot::getProductId, productId));
    }
}
